package com.wgg.common.codec;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

/**
 * @author WSH
 */
public class JdkSerializer implements Serializer {
	
	public static JdkSerializer INSTANCE = new JdkSerializer();
	
	private JdkSerializer () {}

	@Override
	public Object deserialize(InputStream in) throws IOException, ClassNotFoundException {
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(in);
			Object obj = ois.readObject();
			ois.close();
			return obj;
		} finally {
			if (null != ois) {
				try {
					ois.close();
				} catch (Exception e1) {
				}
			}
		}
	} 
	
	@Override
	public void serialize(OutputStream out, Object obj) throws IOException {
		ObjectOutputStream ops = new ObjectOutputStream(out);
		ops.writeObject(obj);
		ops.flush();
	}
}
